package jjn.round1;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author Jiang Jining
 * @since 2022-11-15 23:02
 */
public class LeetCode1710_MaximumUnitsOnATruck {
    public int maximumUnits(int[][] boxTypes, int truckSize) {
        Arrays.sort(boxTypes, Comparator.comparingInt(i -> i[1]));
        int rest = truckSize;
        int total = 0;
        for (int i = boxTypes.length - 1; i >= 0; i--) {
            int num = boxTypes[i][0];
            int size = boxTypes[i][1];
            for (int j = num; j > 0; j--) {
                rest -= 1;
                if (rest >= 0) {
                    total += size;
                }
            }
        }
        return total;
    }
}
